package com.aptech.forms.tdkhoa;

import com.aptech.beans.Users;
import com.aptech.dao.UserManager;
import com.aptech.validate.AccountValidator;
import java.math.BigInteger;
import java.security.*;
import java.sql.*;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.RowSorter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class AccountList extends javax.swing.JFrame {

    public AccountList() {
        initComponents();
        setLocationRelativeTo(null);
        loadData();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jdAccList_InsertUpdate = new javax.swing.JDialog();
        jPanel1 = new javax.swing.JPanel();
        jlblAccList_Username = new javax.swing.JLabel();
        jtfAccList_Username = new javax.swing.JTextField();
        jlblAccList_Password = new javax.swing.JLabel();
        jtfAccList_Password = new javax.swing.JTextField();
        jlblAccList_Email = new javax.swing.JLabel();
        jtfAccList_Email = new javax.swing.JTextField();
        jlblAccList_Address = new javax.swing.JLabel();
        jtfAccList_Address = new javax.swing.JTextField();
        jlblAccList_DateOfBirth = new javax.swing.JLabel();
        cbMonth = new javax.swing.JComboBox();
        cbDay = new javax.swing.JComboBox();
        cbYear = new javax.swing.JComboBox();
        jlblAccList_Email1 = new javax.swing.JLabel();
        cbRole = new javax.swing.JComboBox();
        jbtAccList_Reset = new javax.swing.JButton();
        jbtAccList_Submit = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jdAccList_Notification = new javax.swing.JDialog();
        jLabel3 = new javax.swing.JLabel();
        jlblUsername1 = new javax.swing.JLabel();
        jlblUsername2 = new javax.swing.JLabel();
        jlblUsername3 = new javax.swing.JLabel();
        jlblUsername4 = new javax.swing.JLabel();
        jlblStatus_Username = new javax.swing.JLabel();
        jlblStatus_Email = new javax.swing.JLabel();
        jlblStatus_Password = new javax.swing.JLabel();
        jlblStatus_Address = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbAccountList = new javax.swing.JTable();
        jbtAccountListInsert = new javax.swing.JButton();
        jbtAccountListUpdate = new javax.swing.JButton();
        jbtAccountListDelete = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jtfAccList_Search = new javax.swing.JTextField();
        jlblAccList_Username1 = new javax.swing.JLabel();

        jdAccList_InsertUpdate.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jdAccList_InsertUpdate.setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI Light", 1, 14), new java.awt.Color(255, 0, 0))); // NOI18N

        jlblAccList_Username.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Username.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Username.setText("User name");

        jtfAccList_Username.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jlblAccList_Password.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Password.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Password.setText("Password");

        jtfAccList_Password.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jlblAccList_Email.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Email.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Email.setText("Email");

        jtfAccList_Email.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jlblAccList_Address.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Address.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Address.setText("Address");

        jtfAccList_Address.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jlblAccList_DateOfBirth.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_DateOfBirth.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_DateOfBirth.setText("Date Of Birth");

        cbMonth.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        cbMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" }));

        cbDay.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        cbDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        cbYear.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        cbYear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1993", "1994", "1995", "1996", "1997", "1998", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013" }));

        jlblAccList_Email1.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Email1.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Email1.setText("Role");

        cbRole.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlblAccList_DateOfBirth)
                    .addComponent(jlblAccList_Address)
                    .addComponent(jlblAccList_Email)
                    .addComponent(jlblAccList_Password)
                    .addComponent(jlblAccList_Username)
                    .addComponent(jlblAccList_Email1))
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jtfAccList_Username)
                        .addComponent(jtfAccList_Email, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                        .addComponent(jtfAccList_Address)
                        .addComponent(jtfAccList_Password)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(cbMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(cbDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(cbYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(cbRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblAccList_Username)
                    .addComponent(jtfAccList_Username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblAccList_Password)
                    .addComponent(jtfAccList_Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblAccList_Email)
                    .addComponent(jtfAccList_Email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblAccList_Address)
                    .addComponent(jtfAccList_Address, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblAccList_DateOfBirth)
                    .addComponent(cbMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblAccList_Email1)
                    .addComponent(cbRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        jbtAccList_Reset.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtAccList_Reset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/016.png"))); // NOI18N
        jbtAccList_Reset.setText("Reset");
        jbtAccList_Reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAccList_ResetActionPerformed(evt);
            }
        });

        jbtAccList_Submit.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtAccList_Submit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/003.png"))); // NOI18N
        jbtAccList_Submit.setText("Submit");
        jbtAccList_Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAccList_SubmitActionPerformed(evt);
            }
        });

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/029.png"))); // NOI18N

        javax.swing.GroupLayout jdAccList_InsertUpdateLayout = new javax.swing.GroupLayout(jdAccList_InsertUpdate.getContentPane());
        jdAccList_InsertUpdate.getContentPane().setLayout(jdAccList_InsertUpdateLayout);
        jdAccList_InsertUpdateLayout.setHorizontalGroup(
            jdAccList_InsertUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jdAccList_InsertUpdateLayout.createSequentialGroup()
                .addContainerGap(170, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(165, 165, 165))
            .addGroup(jdAccList_InsertUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jdAccList_InsertUpdateLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jdAccList_InsertUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jdAccList_InsertUpdateLayout.createSequentialGroup()
                            .addGap(49, 49, 49)
                            .addComponent(jbtAccList_Submit)
                            .addGap(38, 38, 38)
                            .addComponent(jbtAccList_Reset, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap()))
        );
        jdAccList_InsertUpdateLayout.setVerticalGroup(
            jdAccList_InsertUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jdAccList_InsertUpdateLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(480, Short.MAX_VALUE))
            .addGroup(jdAccList_InsertUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jdAccList_InsertUpdateLayout.createSequentialGroup()
                    .addGap(92, 92, 92)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jdAccList_InsertUpdateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jbtAccList_Submit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtAccList_Reset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(92, 92, 92)))
        );

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(153, 0, 153));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/031.png"))); // NOI18N
        jLabel3.setText("Notification");
        jLabel3.setToolTipText("");

        jlblUsername1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblUsername1.setForeground(new java.awt.Color(255, 102, 102));
        jlblUsername1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlblUsername1.setText("Password:");

        jlblUsername2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblUsername2.setForeground(new java.awt.Color(255, 102, 102));
        jlblUsername2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlblUsername2.setText("Address:");

        jlblUsername3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblUsername3.setForeground(new java.awt.Color(255, 102, 102));
        jlblUsername3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlblUsername3.setText("Email:");

        jlblUsername4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblUsername4.setForeground(new java.awt.Color(255, 102, 102));
        jlblUsername4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jlblUsername4.setText("User name:");

        jlblStatus_Username.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblStatus_Username.setForeground(new java.awt.Color(102, 102, 102));
        jlblStatus_Username.setText("field");

        jlblStatus_Email.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblStatus_Email.setForeground(new java.awt.Color(102, 102, 102));
        jlblStatus_Email.setText("field");

        jlblStatus_Password.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblStatus_Password.setForeground(new java.awt.Color(102, 102, 102));
        jlblStatus_Password.setText("field");

        jlblStatus_Address.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblStatus_Address.setForeground(new java.awt.Color(102, 102, 102));
        jlblStatus_Address.setText("field");

        javax.swing.GroupLayout jdAccList_NotificationLayout = new javax.swing.GroupLayout(jdAccList_Notification.getContentPane());
        jdAccList_Notification.getContentPane().setLayout(jdAccList_NotificationLayout);
        jdAccList_NotificationLayout.setHorizontalGroup(
            jdAccList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jdAccList_NotificationLayout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(jLabel3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jdAccList_NotificationLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jdAccList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblUsername4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblUsername1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblUsername3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jlblUsername2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 169, Short.MAX_VALUE)
                .addGroup(jdAccList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblStatus_Username)
                    .addComponent(jlblStatus_Email)
                    .addComponent(jlblStatus_Password)
                    .addComponent(jlblStatus_Address))
                .addContainerGap())
        );
        jdAccList_NotificationLayout.setVerticalGroup(
            jdAccList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jdAccList_NotificationLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel3)
                .addGap(61, 61, 61)
                .addGroup(jdAccList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblUsername4, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStatus_Username))
                .addGap(18, 18, 18)
                .addGroup(jdAccList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblUsername1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStatus_Password))
                .addGap(18, 18, 18)
                .addGroup(jdAccList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblUsername3, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStatus_Email))
                .addGap(18, 18, 18)
                .addGroup(jdAccList_NotificationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblUsername2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStatus_Address))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconImage(new ImageIcon(getClass().getResource("/com/image/icon.png")).getImage());
        setResizable(false);

        jtbAccountList.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jtbAccountList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "User ID", "User Name", "Email", "Address", "Date Of Birth", "Create Date", "Role"
            }
        ));
        jtbAccountList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jtbAccountList);

        jbtAccountListInsert.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtAccountListInsert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/026.png"))); // NOI18N
        jbtAccountListInsert.setText("Insert");
        jbtAccountListInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAccountListInsertActionPerformed(evt);
            }
        });

        jbtAccountListUpdate.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtAccountListUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/027.png"))); // NOI18N
        jbtAccountListUpdate.setText("Update");
        jbtAccountListUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAccountListUpdateActionPerformed(evt);
            }
        });

        jbtAccountListDelete.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtAccountListDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/028.png"))); // NOI18N
        jbtAccountListDelete.setText("Delete");
        jbtAccountListDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAccountListDeleteActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Segoe UI Light", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/009.png"))); // NOI18N
        jLabel1.setText("Account Management");

        jtfAccList_Search.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jtfAccList_Search.setToolTipText("Search by Name");
        jtfAccList_Search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfAccList_SearchActionPerformed(evt);
            }
        });
        jtfAccList_Search.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfAccList_SearchKeyPressed(evt);
            }
        });

        jlblAccList_Username1.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Username1.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Username1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/057.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jlblAccList_Username1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfAccList_Search, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addComponent(jbtAccountListInsert)
                        .addGap(18, 18, 18)
                        .addComponent(jbtAccountListUpdate)
                        .addGap(18, 18, 18)
                        .addComponent(jbtAccountListDelete))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(159, 159, 159)
                        .addComponent(jLabel1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jtfAccList_Search, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblAccList_Username1))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtAccountListInsert)
                    .addComponent(jbtAccountListUpdate)
                    .addComponent(jbtAccountListDelete))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /* Reset button */
    private void jbtAccList_ResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAccList_ResetActionPerformed

        if (flag.equals("insert")) {
            clearForm();
        } else if (flag.equals("update")) {

            jtfAccList_Email.setText(null);
            jtfAccList_Address.setText(null);
            cbMonth.setSelectedIndex(0);
            cbDay.setSelectedIndex(0);
            cbYear.setSelectedIndex(0);
        }
    }//GEN-LAST:event_jbtAccList_ResetActionPerformed

    /* Submit button */
    private void jbtAccList_SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAccList_SubmitActionPerformed

        // Get Date from combobox
        int selectedDay = Integer.parseInt(String.valueOf(cbDay.getSelectedItem()));
        int selectedMonth = Integer.parseInt(String.valueOf(cbMonth.getSelectedItem())) - 1;
        int selectedYear = Integer.parseInt(String.valueOf(cbYear.getSelectedItem()));

        // Convert to timestamp
        Calendar calendar = new GregorianCalendar();
        calendar.set(selectedYear, selectedMonth, selectedDay);
        Timestamp dateOfBirthTimestamp = new Timestamp(calendar.getTimeInMillis());

        // Validate all field
        boolean checkUsername = AccountValidator.validateUsername(jtfAccList_Username.getText().trim());
        boolean checkPassword = AccountValidator.validatePassword(jtfAccList_Password.getText().trim());
        boolean checkEmail = AccountValidator.validateEmail(jtfAccList_Email.getText().trim());
        boolean checkAddress = AccountValidator.validateAddress(jtfAccList_Address.getText().trim());
//        boolean checkUsername = true, checkPassword = true, checkEmail = true, checkAddress = true;
        // Check valid fields
        try {
            // Dispose Notification
            jdAccList_Notification.dispose();

            // Encrypt password to md5
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();

            String password = jtfAccList_Password.getText().trim();
            messageDigest.update(password.getBytes(),
                    0, jtfAccList_Password.getText().length());

            String md5 = new BigInteger(1, messageDigest.digest()).toString(16);

            // Get data
            Users user = new Users();

            user.setUserName(jtfAccList_Username.getText().trim());
            user.setPassword(md5);
            user.setEmail(jtfAccList_Email.getText().trim());
            user.setAddress(jtfAccList_Address.getText().trim());
            user.setDateOfBirth(dateOfBirthTimestamp);
            user.setCreateDate(new Timestamp(new Date().getTime()));
            // get selected item combobox Role
            Object item = cbRole.getSelectedItem();;
            user.setRoleID(Integer.parseInt(((ComboItem) item).getValue()));
            // Insert a user
            if (flag.equals("insert")) {
                if (checkUsername
                        && checkPassword
                        && checkEmail
                        && checkAddress) {
                    if (UserManager.checkUsername(jtfAccList_Username.getText().trim()) == null) {
//                        System.out.println(jtfAccList_Username.getText());
//                        System.out.println(UserManager.getUserID(jtfAccList_Username.getText()));
                        boolean result = UserManager.insert(user);
                        if (result) {
//                            System.out.println(UserManager.getUserID(jtfAccList_Username.getText()).getUserID());
                            JOptionPane.showMessageDialog(this, "Insert successfully!", "Message", JOptionPane.INFORMATION_MESSAGE);

                            loadData();
                            jdAccList_InsertUpdate.dispose();
                        } else {
                            JOptionPane.showMessageDialog(this, "Insert failed!", "Message", JOptionPane.ERROR_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, "Existing Username", "Message", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    jdAccList_Notification.setVisible(true);
                    jdAccList_Notification.pack();

                    ImageIcon correctIcon = new javax.swing.ImageIcon(getClass().getResource("/com/image/032.png"));
                    ImageIcon falseIcon = new javax.swing.ImageIcon(getClass().getResource("/com/image/033.png"));

                    jlblStatus_Username.setIcon(checkUsername ? correctIcon : falseIcon);
                    jlblStatus_Password.setIcon(checkPassword ? correctIcon : falseIcon);
                    jlblStatus_Email.setIcon(checkEmail ? correctIcon : falseIcon);
                    jlblStatus_Address.setIcon(checkAddress ? correctIcon : falseIcon);
                }

            } else if (flag.equals("update")) {
                if (checkEmail
                        && checkAddress) {
                    boolean result = UserManager.update(user);

                    if (result) {
                        JOptionPane.showMessageDialog(this, "Update successfully!", "Message", JOptionPane.INFORMATION_MESSAGE);

                        loadData();
                        jdAccList_InsertUpdate.dispose();
                    } else {
                        JOptionPane.showMessageDialog(this, "Update failed!", "Message", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    jdAccList_Notification.setVisible(true);
                    jdAccList_Notification.pack();

                    ImageIcon correctIcon = new javax.swing.ImageIcon(getClass().getResource("/com/image/032.png"));
                    ImageIcon falseIcon = new javax.swing.ImageIcon(getClass().getResource("/com/image/033.png"));

//                    jlblStatus_Username.setIcon(checkUsername ? correctIcon : falseIcon);
//                    jlblStatus_Password.setIcon(checkPassword ? correctIcon : falseIcon);
                    jlblStatus_Email.setIcon(checkEmail ? correctIcon : falseIcon);
                    jlblStatus_Address.setIcon(checkAddress ? correctIcon : falseIcon);
                }

            }

        } catch (NoSuchAlgorithmException ex) {
            System.err.println(ex.getMessage());
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
        }
    }//GEN-LAST:event_jbtAccList_SubmitActionPerformed

    /* Insert data */
    private void jbtAccountListInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAccountListInsertActionPerformed
        jdAccList_InsertUpdate.setVisible(true);
        jdAccList_InsertUpdate.pack();
        jdAccList_InsertUpdate.setLocationRelativeTo(null);
        jtfAccList_Username.setEditable(true);
        jtfAccList_Password.setEditable(true);
        flag = "insert";
        clearForm();
    }//GEN-LAST:event_jbtAccountListInsertActionPerformed

    /* Update data */
    private void jbtAccountListUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAccountListUpdateActionPerformed

        flag = "update";
        clearForm();
        if (jtbAccountList.getSelectedRowCount() == 1) {
            jdAccList_InsertUpdate.setVisible(true);
            jdAccList_InsertUpdate.pack();
            jdAccList_InsertUpdate.setLocationRelativeTo(null);
            jtfAccList_Username.setEditable(false);
            jtfAccList_Password.setEditable(false);
            try {
                ResultSet resultSet = UserManager.displayAllRows();
                resultSet.absolute(jtbAccountList.getSelectedRow() + 1);
                jtfAccList_Username.setText(resultSet.getString(2).trim());
//                jtfAccList_Password.setText(resultSet.getString(3).trim());
                jtfAccList_Email.setText(resultSet.getString(4).trim());
                jtfAccList_Address.setText(resultSet.getString(5).trim());

                String getDate = resultSet.getTimestamp(6).toString().substring(0, 10);
                String[] str = getDate.split("\\-");

                cbYear.setSelectedItem(str[0]);
                cbMonth.setSelectedItem(str[1]);
                cbDay.setSelectedItem(str[2]);
                cbRole.setSelectedIndex(UserManager.checkRoleByRoleName(resultSet.getString(8)).getRoleID() - 1);
            } catch (SQLException ex) {
                System.err.println(ex.getMessage());
            }
        } else {
            JOptionPane.showMessageDialog(null, "Please choose a record to update", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jbtAccountListUpdateActionPerformed
    /**
     * *********
     */
    /* Delete data */
    private void jbtAccountListDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAccountListDeleteActionPerformed
        if (jtbAccountList.getSelectedRowCount() == 1) {
            int reply = JOptionPane.showConfirmDialog(null, "Do you want to delete this record!",
                    "Message", JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION) {
                try {
                    ResultSet resultSet = UserManager.displayAllRows();
                    resultSet.absolute(jtbAccountList.getSelectedRow() + 1);
                    resultSet.deleteRow();
                    loadData();
                } catch (SQLException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }
    }//GEN-LAST:event_jbtAccountListDeleteActionPerformed

    /*
     * Searching data
     */
    private void jtfAccList_SearchKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfAccList_SearchKeyPressed
        jtfAccList_Search.getDocument().addDocumentListener(
                new DocumentListener() {
                    @Override
                    public void changedUpdate(DocumentEvent e) {
                        newFilter();
                    }

                    @Override
                    public void insertUpdate(DocumentEvent e) {
                        newFilter();
                    }

                    @Override
                    public void removeUpdate(DocumentEvent e) {
                        newFilter();
                    }

                    private void newFilter() {
                        TableRowSorter sorter = new TableRowSorter<>(tableModel);
                        jtbAccountList.setRowSorter(sorter);
                        // Fillter data by cloumn = 1
                        RowFilter<DefaultTableModel, Object> rowFilter = RowFilter.regexFilter(jtfAccList_Search.getText(), 1);
                        sorter.setRowFilter(rowFilter);
                    }
                });
    }//GEN-LAST:event_jtfAccList_SearchKeyPressed

    private void jtfAccList_SearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfAccList_SearchActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfAccList_SearchActionPerformed

    /* Load data to table */
    private void loadData() {
        tableModel = (DefaultTableModel) jtbAccountList.getModel();
        tableModel.setRowCount(0);
        try {

            // Fill all record from users table
            ResultSet resultSet = UserManager.displayAllRows();
            while (resultSet.next()) {
                Vector vector = new Vector();
                vector.add(String.valueOf(resultSet.getInt(1)).trim());
                vector.add(resultSet.getString(2).trim());
                vector.add(resultSet.getString(4).trim());
                vector.add(resultSet.getString(5).trim());
                vector.add(String.valueOf(resultSet.getDate(6)).trim());
                vector.add(String.valueOf(resultSet.getDate(7)).trim());
                vector.add(resultSet.getString(8));
                tableModel.addRow(vector);

            }

            // Fill data into combobox cbRole
            if (cbRole.getItemCount() == 0) {
                ResultSet rsRoles = UserManager.getRoles();
                while (rsRoles.next()) {
                    cbRole.addItem(new ComboItem(rsRoles.getString(2), String.valueOf(rsRoles.getInt(1)).trim()));
                }
            }

//            cbRole.addItem(new ComboItem("Visible String 1", "Value 1"));
//
//            Object item = cbRole.getSelectedItem();
//            String str = ((ComboItem) item).getValue();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    class ComboItem {

        private String key;
        private String value;

        public ComboItem(String key, String value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public String toString() {
            return key;
        }

        public String getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }
    }

    /* Clearn form */
    private void clearForm() {
        jtfAccList_Username.setText(null);
        jtfAccList_Password.setText(null);
        jtfAccList_Email.setText(null);
        jtfAccList_Address.setText(null);
        cbMonth.setSelectedIndex(0);
        cbDay.setSelectedIndex(0);
        cbYear.setSelectedIndex(0);
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AccountList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AccountList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AccountList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AccountList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AccountList().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbDay;
    private javax.swing.JComboBox cbMonth;
    private javax.swing.JComboBox cbRole;
    private javax.swing.JComboBox cbYear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtAccList_Reset;
    private javax.swing.JButton jbtAccList_Submit;
    private javax.swing.JButton jbtAccountListDelete;
    private javax.swing.JButton jbtAccountListInsert;
    private javax.swing.JButton jbtAccountListUpdate;
    private javax.swing.JDialog jdAccList_InsertUpdate;
    private javax.swing.JDialog jdAccList_Notification;
    private javax.swing.JLabel jlblAccList_Address;
    private javax.swing.JLabel jlblAccList_DateOfBirth;
    private javax.swing.JLabel jlblAccList_Email;
    private javax.swing.JLabel jlblAccList_Email1;
    private javax.swing.JLabel jlblAccList_Password;
    private javax.swing.JLabel jlblAccList_Username;
    private javax.swing.JLabel jlblAccList_Username1;
    private javax.swing.JLabel jlblStatus_Address;
    private javax.swing.JLabel jlblStatus_Email;
    private javax.swing.JLabel jlblStatus_Password;
    private javax.swing.JLabel jlblStatus_Username;
    private javax.swing.JLabel jlblUsername1;
    private javax.swing.JLabel jlblUsername2;
    private javax.swing.JLabel jlblUsername3;
    private javax.swing.JLabel jlblUsername4;
    private javax.swing.JTable jtbAccountList;
    private javax.swing.JTextField jtfAccList_Address;
    private javax.swing.JTextField jtfAccList_Email;
    private javax.swing.JTextField jtfAccList_Password;
    private javax.swing.JTextField jtfAccList_Search;
    private javax.swing.JTextField jtfAccList_Username;
    // End of variables declaration//GEN-END:variables
    private String flag = "";
    DefaultTableModel tableModel;
}
